package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Dept;
import com.itheima.domain.Result;
import com.itheima.DeptService;
import com.itheima.utils.BeanUtil;
import com.itheima.web.filters.BeanFactory;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * http://localhost:8080/heima_mm/system/dept/findByPage?currentPage=1&Size=5
 * 可以接受来自system/dept下面多有的方法
 */
@WebServlet("/system/dept/*")
public class DeptServlet extends BaseServlet {
    private DeptService deptService = BeanFactory.getBean("deptService",DeptService.class);
    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
//        获取请求参数
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
//        判断非空
        int currentPage = 1,pageSize=5;//定义默认的页码和每页条数
        if (!StringUtils.isBlank(_currentPage)){
            currentPage = Integer.parseInt(_currentPage);
        }if (!StringUtils.isBlank(_pageSize)){
             pageSize = Integer.parseInt(_pageSize);
        }
//        调用service层方法获取分页结果
        PageInfo<Dept> info = deptService.findByPage(currentPage, pageSize);
//        对客户做出响应
        return new Result(true, MessageConstant.QUERY_CATALOG_SUCCESS,info);

    }
    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
//        获取请求参数并封装成Dept对象
        try {
            Dept dept = BeanUtil.fillBeanFromJson(request, Dept.class);
//            调用service层方法添加数据
            deptService.save(dept);
//        响应结果
            return new Result(true,MessageConstant.SAVE_COMPANY_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }

    }
    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
//        获得请求参数
            String deptId = request.getParameter("id");
//            调用Service层方法
            Dept dept = deptService.findById(deptId);
//       对客户做出响应
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,dept);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }

    }
    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
//        获取请求参数
        try {
            Dept dept = BeanUtil.fillBeanFromJson(request, Dept.class);
//       调用serice层方法，修改数据
            deptService.update(dept);
//        响应结果
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_COMPANY_FAIL);
        }
    }
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
//        获取请求参数，封装成对象
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
//        调用service层方法，删除数据
            deptService.delete(ids);
//        响应结果
            return new Result(true,MessageConstant.DELETE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_COMPANY_FAIL);
        }

    }
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
//        调用service层方法，调查所有数据
            List<Dept> list = deptService.findAll();
//        响应结果
            return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_COMPANY_FAIL);
        }

    }

}